import javax.swing.tree.TreeNode;
import java.util.*;

public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;//左孩子的引用
        public TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    /**
     * 创建一棵二叉树 返回这棵树的根节点
     *
     * @return
     */
    public TreeNode createTree() {
        TreeNode a = new TreeNode('A');
        TreeNode b = new TreeNode('B');
        TreeNode b1 = new TreeNode('B');
        TreeNode c = new TreeNode('C');
        TreeNode d = new TreeNode('D');
        TreeNode e = new TreeNode('E');
        TreeNode f = new TreeNode('F');
        TreeNode g = new TreeNode('G');
        TreeNode h = new TreeNode('H');
//
        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        e.right = h;
        c.right = g;
        c.left = f;

//        a.left = b;
//        a.right = b;
//        b1.left = c;
//        b1.right = d;
//        b.left = d;
//        b.right = c;


        return a;
    }

    public int i = 0;
    public TreeNode createTree(String str){
        TreeNode root = null;
        if(str.charAt(i) != '#'){
            root = new TreeNode(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else{
            i++;
        }
        return root;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public List<Character> preOrderTraversal1(TreeNode root) {
        List<Character> res = new ArrayList<Character>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while(root != null || !stack.isEmpty()) {
            if(root != null) {
                stack.push(root);
                res.add(root.val);
                root = root.left;
            } else {
                root = stack.pop();
                root = root.right;
            }
        }
        return res;
    }


    List<Character> list = new ArrayList<>();
    public List<Character> preorderTraversal(TreeNode root) {
        if(root == null){
            return null;
        }

        list.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);

        return list;
    }

    // 中序遍历
    public void inOrder(TreeNode root) {
        if(root == null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    List<Character> list1 = new ArrayList<>();
    public List<Character> inOrderTraversal(TreeNode root) {
        if(root == null){
            return null;
        }

        inOrderTraversal(root.left);
        list1.add(root.val);
        inOrderTraversal(root.right);

        return list1;
    }

    public List<Character> InorderTraversal(TreeNode root) {
        List<Character> res = new ArrayList<Character>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        while(root != null || !stack.isEmpty()) {
            if(root != null) {
                stack.push(root);
                root = root.left;
            } else {
                root = stack.pop();
                res.add(root.val);
                root = root.right;
            }
        }
        return res;
    }


    // 后序遍历
    public void postOrder(TreeNode root) {
        if(root == null){
            return;
        }
        preOrder(root.left);
        preOrder(root.right);
        System.out.print(root.val + " ");
    }

    List<Character> list2 = new ArrayList<>();
    public List<Character> postOrderTraversal(TreeNode root) {
        if(root == null){
            return null;
        }

        postOrderTraversal(root.left);
        list2.add(root.val);
        postOrderTraversal(root.right);

        return list2;
    }

    public static int nodeSize = 0;

    /**
     * 获取树中节点的个数：遍历思路
     */

    public void size(TreeNode root) {
        if (root == null){
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }

    /**
     * 获取节点的个数：子问题的思路
     *
     * @param root
     * @return
     */
    public int size2(TreeNode root) {
        if (root == null){
            return 0;
        }

        return size2(root.right) + size2(root.left) + 1;
        /*int Size = 1;
        int leftSize = size2(root.left);
        Size += leftSize;
        int rightSize = size2(root.right);
        Size += rightSize;

        return Size;*/
    }

    /*
     获取叶子节点的个数：遍历思路
     */
    public static int leafSize = 0;

    public void getLeafNodeCount1(TreeNode root) {
        if(root == null){
            return;
        }
        if((nodeSize % 2) == 0){
            leafSize = nodeSize / 2;
        }else {
            leafSize = nodeSize / 2 - 1;
        }
    }

    /*
     获取叶子节点的个数：子问题
     */
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null){
            return 0;
        }

        return (root.right == null && root.left == null)
                ? 1
                : getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    public static int leafSize1 = 0;
    public void getLeafNodeCount3(TreeNode root) {
        if(root == null){
            return;
        }
        if(root.right == null && root.left == null){
            leafSize1++;
        }
        getLeafNodeCount3(root.left);
        getLeafNodeCount3(root.right);
    }

    /*
    获取第K层节点的个数
     */
    public int getKLevelNodeCount(TreeNode root, int k) {
        if(root == null){
            return 0;
        }
        return k == 1 ? 1 : getKLevelNodeCount(root.left,k-1)+
                getKLevelNodeCount(root.right,k-1);
    }

    /*
     获取二叉树的高度
     时间复杂度：O(N)
     */
    public int getHeight(TreeNode root) {
        if(root == null){
            return 0;
        }

        return Math.max(getHeight(root.left) , getHeight(root.right)) + 1;

        //会重复计算很多
        //
        /*return getHeight(root.left) > getHeight(root.right)
                ? getHeight(root.left) + 1
                : getHeight(root.right) + 1;*/


    }

    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val) {
        if(root == null){
            return null;
        }

        /*if(root.val == val){
            return root;
        }else if(root.left.val == val){
            return root.left;
        }else if(root.right.val == val){
            return root.right;
        }else{
            find(root.left,val);
            find(root.right,val);
        }*/

        TreeNode leftT = find(root.left,val);
        if(leftT != null){
            return root.left;
        }
        TreeNode rightT = find(root.right,val);
        if(rightT != null){
            return root.left;
        }
        return null;
    }

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }
        if(isSameTree(root,subRoot)){
            return true;
        }

        return isSubtree(root.left,subRoot) || isSubtree(root.right,subRoot);

        // if(isSameTree(root,subRoot)){
        //     return true;
        // }
        // if(isSubtree(root.left,subRoot)) {
        //     return true;
        // }
        // if(isSubtree(root.right,subRoot)) {
        //     return true;
        // }
        // return false;
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p != null && q == null || p == null && q != null){
            return false;
        }
        if(p == null && q == null){
            return true;
        }
        if(p.val != q.val){
            return false;
        }

        return isSameTree(q.left,p.left) && isSameTree(q.right,p.right);
    }

    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return root;
        }

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }

    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }

        return isSymmetricChilh(root.left,root.right);
    }
    public boolean isSymmetricChilh(TreeNode LeftNode,TreeNode RightNode) {
        if(LeftNode == null && RightNode == null){
            return true;
        }

        if(LeftNode == null || RightNode == null){
            return false;
        }

        return LeftNode.val == RightNode.val
                && isSymmetricChilh(LeftNode.left,RightNode.right)
                && isSymmetricChilh(LeftNode.right,RightNode.left);
    }



    //层序遍历
    public void levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }
        System.out.println();
    }

    // 判断一棵树是不是完全二叉树
    public boolean isCompleteTree(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur == null){
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }

        while (!queue.isEmpty()){
            TreeNode peek = queue.peek();
            if(peek != null){
                return false;
            }
            queue.poll();
        }
        return true;
    }
}